package com.psbc.data.trans.common.utils;

import cn.hutool.db.meta.Column;
import cn.hutool.db.meta.MetaUtil;
import cn.hutool.db.meta.Table;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;

public class DBUtils {

    public static void main(String[] args) {
        DataSource ds = DataSourceBuilder.create()
                .driverClassName("org.postgresql.Driver")
                .url("jdbc:postgresql://139.196.217.100:5432/bzydb")
                .username("postgres")
                .password("admin@123")
                .build();

        List<String> tables = MetaUtil.getTables(ds);
        System.out.println(tables);

        String tableName = tables.get(0);
        Table tableMeta = MetaUtil.getTableMeta(ds, tableName);

        Collection<Column> columns = tableMeta.getColumns();
        columns.forEach(column -> {
            System.out.print(column.getName()+",");
        });

        //获取表的所有字段
//        columns.forEach(column -> {
//            if(column.getTypeName().equals("varchar")){
//                System.out.println(column.getName()+":"+column.getTypeName()+"("+column.getSize()+")");
//            }else {
//                System.out.println(column.getName()+":"+column.getTypeName());
//            }
//        });

    }

}
